package com.gy.paidao.util;


import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.gy.paidao.R;


/**
 * @author adrainy 自定义弹出框
 */
public class ShowDialogUtil {

    private static Dialog dialog = null;

    // 用来控制防止点击出多个dialog
    private static boolean isShowing;


    /**
     * 提示框
     *
     * @param c
     * @param title        展示信息的title
     * @param msg          展示的信息
     * @param ok
     * @param cancel       如果不显示取消按钮，值 null
     * @param listener     点击确定或者取消，执行的listener
     */
    public static Dialog showDialog(Activity c, int title, Spannable msg,
                                    int ok, int cancel, final OnClickListener listener) {
        if (isShowing)
            return null;
        isShowing = true;


        try {
            dialog = new Dialog(c, R.style.CustomDialogStyle);
            LayoutInflater mInflater = LayoutInflater.from(c);
            View view = mInflater.inflate(R.layout.dialog_show_money_layout, null);

            TextView tvMsg = (TextView) view
                    .findViewById(R.id.dialog_show_money_tv_msg);
            TextView btnOK = (TextView) view
                    .findViewById(R.id.dialog_show_money_btn_ok);
            TextView btnCancel = (TextView) view
                    .findViewById(R.id.dialog_show_money_btn_cancel);

            TextView tvTitle = (TextView) view
                    .findViewById(R.id.dialog_show_money_tv_title);
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);


            btnOK.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    cancelDialog();
                    if (listener != null) {
                        listener.onClick(v);
                    }
                }
            });

            tvMsg.setText(msg);
            btnOK.setText(ok);
            if (cancel == 0) {
                btnCancel.setVisibility(View.GONE);
            } else {
                btnCancel.setText(cancel);
                btnCancel.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cancelDialog();
                        if (listener != null) {
                            listener.onClick(v);
                        }
                    }
                });
            }
            dialog.setContentView(view);
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    isShowing = false;
                }
            });

            if (!c.isFinishing()) {
                dialog.show();
            } else {
                isShowing = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dialog;
    }

    public static void cancelDialog() {
        try {
            if (dialog != null && dialog.isShowing())
                dialog.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}